package com.creative.accountsdk;

import android.app.Application;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.creative.accountsdk.net.Protocol;

/**
 * Created by yinjialiang on 15-8-28.
 */
public class UmsChannel {
    private final static String[] APP_KEYS_TEST = {"36e8b04de845cb8b28912afdea391ee0", "64fe25c9109dd23f83b56aef6f3606a8", "1522c48601d2d5e3cbb8f64e77cf5d43", "61af5f9ffaac83b782530072f3caea74", "ef27c99f5244625afa5300f7083a5c5a",
            "d9f6a6e112b57085719b78bbd9f7c706", "ac60c6f451e2faf2cfdbc456f38c7fb7", "a65a3f4767e002630d235b08417a2946", "742853b465f42401cc1fbaebbf5fdbd8"};

    private final static String[] APP_KEYS_PUBLIC_TEST = {"c2a23b2a857106c61286c8ea8662ce12", "6daa60d6f14048f286aab850cc3c8f43", "73b4cf6722d894cf899828c81a17c8f4", "afe4943d1e87698de1f6d4eb9a7dfaa4", "a8e310bb808ab3e4f3d5c587029a2aff",
            "aaa5f88e50466f3f8bfc52ec0a55de77", "a57080065876f692fa226873bae6e6e1", "527e623b59eae1386f0c093684e040b0", "1034ee9b91dd44b31b9c7d88835c9f3c"};

    private final static String[] APP_KEYS_PUBLIC = {"6b6d867eb7c524c8c09b86dd58227394", "f52c3b3c81d34789a8e15e0d5f8b2e32", "dabad737102ac48e03b9ca53392a80a5", "5f1e5e6673c7a9b7b3686201a9f63675", "b2c8afed98ef00b03a6c9cfcc2d17ced",
            "e28b4649a6670d12530598cb262d67ac", "cc86aaa9d75eb068b96253d6c1181905", "ccba5b9bb0ea9749f1424792b98ffb2c", "68ed8d5c8e4e5110b642f781da184b31"};

    private final static String[] CHANNELS = {"qq", "weixin", "weibo", "bbs", "baidu_zhidao",
            "official", "zazhipu", "app_in_share", "jrtt"};

    public final static String PARAM_NAME = "channel";

    private static String umsChannel;
    public static String getChannel()
    {
        if(umsChannel == null) {
            try {
                Application application = AccountSdk.getApplication();
                ApplicationInfo applicationInfo = application.getPackageManager().getApplicationInfo(application.getPackageName(), PackageManager.GET_META_DATA);
                String umsAppkey = applicationInfo.metaData.getString("UMS_APPKEY");
                String[] appKeys = null;
                switch (Protocol.SERVER)
                {
                    case Protocol.SERVER_TEST:
                        appKeys = APP_KEYS_TEST;
                        break;
                    case Protocol.SERVER_PUBLIC_TEST:
                        appKeys = APP_KEYS_PUBLIC_TEST;
                        break;
                    case Protocol.SERVER_PUBLIC:
                        appKeys = APP_KEYS_PUBLIC;
                        break;
                }
                for(int i = 0; i < appKeys.length; i++)
                {
                    if(umsAppkey.equals(appKeys[i]))
                    {
                        umsChannel = CHANNELS[i];
                        break;
                    }
                }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        Log.d(UmsChannel.class.getSimpleName(), "getChannel: " + umsChannel);
        return umsChannel;
    }


}
